package com.csair.platform.cit.service.biz.impl;

import com.csair.platform.cit.facade.model.citJscg.CitJscgRequest;
import com.csair.platform.cit.facade.model.citJscg.CitJscgVo;
import com.csair.platform.cit.service.biz.ICitJscgService;
import com.csair.platform.cit.service.dao.CitJscgRepository;
import com.csair.platform.cit.service.entity.CitJscg;
import com.csair.platform.cit.service.util.JsonHelper;
import com.csair.platform.cit.service.util.ValidatorUtils;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zhengxincheng
 * 技术成果投资入股企业所得税递延纳税备案表
 */
@Service
@Slf4j
public class CitJscgServiceImpl extends InitBaseServiceImpl implements ICitJscgService {

    @Autowired
    CitJscgRepository citJscgRepository;

    /**
     * 申报报文前缀
     */
    private static final String POST_FIX ="ht_.ywbw.dynsbabYwbd.dynsbabGrid.dynsbabGridlb";

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(CitJscgRequest sjr) {
        ValidatorUtils.validateEntity(sjr);
        ValidatorUtils.validateList(sjr.getItem());

        deleteByNsrsbhAndSkssqqAndSkssqz(sjr.getNsrsbh(),sjr.getSkssqq(),sjr.getSkssqz());
        List<CitJscg> citJscgs = JsonHelper.from(sjr.getItem(), new TypeToken<List<CitJscg>>() {}.getType());

        citJscgRepository.saveAll(citJscgs);
    }

    @Override
    public CitJscgRequest queryByNsrsbhAndSkssqqAndSkssqz(CitJscgRequest cjq) {

        ValidatorUtils.validateEntity(cjq);

        List<CitJscg> cjs = citJscgRepository.queryByNsrsbhAndSkssqqAndSkssqz(cjq.getNsrsbh(),cjq.getSkssqq(),cjq.getSkssqz());
        List<CitJscgVo> cjv = JsonHelper.from(cjs, new TypeToken<List<CitJscgVo>>() {}.getType());
        if(CollectionUtils.isEmpty(cjs)){
            cjv = initCitJscg();
        }
        cjq.setItem(cjv);
        cjq.setFddbrqz(cjv.get(0).getFddbrqz());
        cjq.setTbr(cjv.get(0).getTbr());
        cjq.setNsrmc(cjv.get(0).getNsrmc());
        cjq.setTbrq(cjv.get(0).getTbrq());
        return cjq;
    }

    private List<CitJscgVo> initCitJscg() {
        List<CitJscgVo> cjvs = new ArrayList<CitJscgVo>();
        CitJscgVo cjv = new CitJscgVo("1","","","","0","0","","","","","","","","","",null);
        CitJscgVo cjv1 = new CitJscgVo("合计","--","--","--","0","0","--","--","--","--","--","--","--","","",null);

        cjvs.add(cjv);
        cjvs.add(cjv1);

        return cjvs;
    }

    @Override
    public void deleteByNsrsbhAndSkssqqAndSkssqz(String nsrsbh, String skssqq, String skssqz) {
        citJscgRepository.deleteByNsrsbhAndSkssqqAndSkssqz(nsrsbh,skssqq,skssqz);
    }

    @Override
    public List<CitJscg> queryByNsrsbhAndSkssqqAndSkssqz(String nsrsbh, String skssqq, String skssqz ){
        CitJscg query = new CitJscg();
        query.setNsrsbh(nsrsbh);
        query.setSkssqq(skssqq);
        query.setSkssqz(skssqz);
        Example<CitJscg> example = Example.of(query);
        List<CitJscg> jscgs = citJscgRepository.findAll(example);
        return jscgs;
    }
}
